package panel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.RowSorter;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;
import javax.swing.table.TableModel;
import javax.swing.table.TableRowSorter;

import file.IOStreamStudent;
import model.Student;

public class ChangeTable extends JFrame{
	public static void update(){
		ScorePage.instance.setVisible(false);
		String column[]=new String[]{"学号","姓名","考勤","测验","作业","期末","总评"};
		JTable score=new JTable();
		DefaultTableModel model;
		JScrollPane scroll=new JScrollPane(score);
		score = new JTable(); //创建一个表显示数据
        score.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        score.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        score.setRowHeight(30);
        JTableHeader header = score.getTableHeader(); // 获取 JTable 的头的对象
        header.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        header.setPreferredSize(new Dimension(header.getWidth(), 35)); // 设置此组件的首选大小

        model = (DefaultTableModel) score.getModel();
        model.setColumnIdentifiers(column);
        //CourseManage.findOneStudentAllCourseMark(stu);
        ArrayList<Student> list=new ArrayList<>();
        list=IOStreamStudent.readFromFile();
        String[][] student=new String[list.size()][7];
        for(int i=0;i<list.size();i++){
        	for(int j=0;j<7;j++){
        		if(j==0)
        			student[i][j]=list.get(i).getXueHao();
        		if(j==1)
        			student[i][j]=list.get(i).getName();
        		if(j==2)
        			student[i][j]=list.get(i).getKaoQing();
        		if(j==3)
        			student[i][j]=list.get(i).getCeYan();
        		if(j==4)
        			student[i][j]=list.get(i).getZuoYe();
        		if(j==5)
        			student[i][j]=list.get(i).getQiMo();
        		if(j==6)
        			student[i][j]=list.get(i).getZongJi();
        	}
        }
        //JTable jTable=new JTable(student, column);
        TableModel tableModel = new DefaultTableModel(student, column);
        JTable table = new JTable(tableModel);
     // 使用 表格模型 创建 行排序器（TableRowSorter 实现了 RowSorter）
        RowSorter<TableModel> rowSorter = new TableRowSorter<TableModel>(tableModel);

        // 给 表格 设置 行排序器
        table.setRowSorter(rowSorter);
        
        scroll.setViewportView(table);
        new ScorePage().add(scroll);
        new ScorePage().setVisible(true);
        ScorePage.instance.updateUI();
	}
}
